$(document).ready(function(){
    //没有在文件或者文件夹上右击时的事件
    $("#c_body").contextmenu(function(e){
        var actives = $(".dir_file_active");
        actives.each(function(index,item){
            $(item).removeClass("dir_file_active");
        });
        $(".file_menu").hide();
        $(".dir_menu").hide();
        var winWidth = $(document).width();

        var winHeight = $(document).height();

        var posX = e.pageX;

        var posY = e.pageY;

        var menuWidth = $(".contextmenu").width();

        var menuHeight = $(".contextmenu").height();

        var secMargin = 10;

        if(posX + menuWidth + secMargin >= winWidth

            && posY + menuHeight + secMargin >= winHeight){

            posLeft = posX - menuWidth - secMargin + "px";

            posTop = posY - menuHeight - secMargin + "px";

        }

        else if(posX + menuWidth + secMargin >= winWidth){

            posLeft = posX - menuWidth - secMargin + "px";

            posTop = posY + secMargin + "px";

        }

        else if(posY + menuHeight + secMargin >= winHeight){

            posLeft = posX + secMargin + "px";

            posTop = posY - menuHeight - secMargin + "px";

        }

        else {

            posLeft = posX + secMargin + "px";

            posTop = posY + secMargin + "px";

        };

        $(".contextmenu").css({

            "left": posLeft,

            "top": posTop

        }).show();
        return false;

    });

    /*整个页面的右键菜单*/
    $("#c_body").on("contextmenu",".file,.dir",(function(e){
        var that = $(this);
        var actives = $(".dir_file_active");
        var menu = null;
        actives.each(function(index,item){
            $(item).removeClass("dir_file_active");
        });
        $(".file_menu").hide();
        $(".dir_menu").hide();
        $(".contextmenu").hide();
        //处理时文件夹还是文件的问题
        if (that.hasClass("file")){
            menu = $(".file_menu");
            $(that).addClass("dir_file_active");
        }else if(that.hasClass("dir")){
            menu = $(".dir_menu");
            $(that).addClass("dir_file_active");
        }else{
            menu = $(".contextmenu");
        }


        var winWidth = $(document).width();

        var winHeight = $(document).height();

        var posX = e.pageX;

        var posY = e.pageY;

        var menuWidth = menu.width();

        var menuHeight = menu.height();

        var secMargin = 10;

        if(posX + menuWidth + secMargin >= winWidth

            && posY + menuHeight + secMargin >= winHeight){

            posLeft = posX - menuWidth - secMargin + "px";

            posTop = posY - menuHeight - secMargin + "px";

        }

        else if(posX + menuWidth + secMargin >= winWidth){

            posLeft = posX - menuWidth - secMargin + "px";

            posTop = posY + secMargin + "px";

        }

        else if(posY + menuHeight + secMargin >= winHeight){

            posLeft = posX + secMargin + "px";

            posTop = posY - menuHeight - secMargin + "px";

        }

        else {

            posLeft = posX + secMargin + "px";

            posTop = posY + secMargin + "px";

        };

        menu.css({

            "left": posLeft,

            "top": posTop

        }).show();
        return false;
    }
    ));
});